﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataObjects.Interface.Person;
using BusinessObjects.Person;
using System.Data;
using DataObjects.AdoNet;

namespace DataObjects.SqlServer.Person
{
    public class SqlServerDistrictsDao : IDistrictDao
    {
        public List<District> GetDistricts()
        {
            string procedures = "Get_All_Districts";

            return Db.ReadList(procedures, true, Make, null);
        }

        public District GetDistrict(string districtCode)
        {
            string procedures = "Get_District_ByCode";

            object[] parms = { "@DistrictCode", districtCode };
            return Db.Read(procedures, true, Make, parms);
        }

        public int GetTotalPage()
        {
            string procedures = "Get_PageSize_District";

            return Db.ReadTotalPage(procedures, true, null);
        }

        public List<District> GetDistrictsByPage(int currentPage)
        {
            string procedures = "Get_Paged_Districts";

            object[] parms = {  "@WhereClause", null, 
                                "@OrderBy", null ,
                                "@PageIndex", currentPage};
            return Db.ReadList(procedures, true, Make, parms);
        }

        public List<District> GetDistrictsByIsActive(bool isActive)
        {
            string procedures = "Get_All_Districts_ByIsActive";

            object[] parms = { "@IsActive", isActive };
            return Db.ReadList(procedures, true, Make, parms);
        }

        public void InsertDistrict(District district)
        {
            string sql = "Insert_District";

            district.DistrictCode = Db.Insert(sql, true, Take(district));
        }

        public void UpdateDistrict(District district)
        {
            string sql = "Update_District";

            Db.Update(sql, true, Take(district));
        }

        public void DeleteDistrict(District district)
        {
            string sql = "Delete_District";

            Db.Update(sql, true, Take(district.DistrictCode));
        }

        private object[] Take(string districtCode)
        {
            return new object[]  
            {
                "@DistrictCode", districtCode,
            };
        }

        private object[] Take(District district)
        {
            return new object[]  
            {
                "@DistrictCode", district.DistrictCode,
                "@DistrictName", district.DistrictName,
                "@CreateDate", district.CreateDate,
                "@ModifiedDate", district.ModifiedDate,
                "@Description", district.Description,
                "@IsActive", district.IsActive
            };
        }

        private static Func<IDataReader, District> Make = reader =>
            new District
            {
                DistrictCode = reader["DistrictCode"].AsString(),
                DistrictName = reader["DistrictName"].AsString(),
                CreateDate = reader["CreateDate"].AsDateTime(),
                ModifiedDate = reader["ModifiedDate"].AsDateTime(),
                Description = reader["Description"].AsString(),
                IsActive = reader["IsActive"].AsBool()
            };
    }
}